<?php

namespace app\common\taglib;

use think\template\TagLib;

class Html extends TagLib {

    /**
     * 定义标签列表
     */
    protected $tags = [
        'select' => ['attr' => 'id,name,options,multiple,size,first,clazz,style,selected', 'close' => 0],
        'checkbox' => ['attr' => 'id,name,checkboxes,multiple,size,first,clazz,style', 'close' => 0],
        'radio' => ['attr' => 'id,name,options,size,clazz,style,checked', 'close' => 0],

        'auth' => ['attr' => 'name', 'close' => 1],

    ];

    /**
     * 下拉框标签
     */
    public function tagSelect($tag) {
        $id = isset($tag['id']) ? $tag['id'] : '';;
        $name = isset($tag['name']) ? $tag['name'] : 'title';
        $options = isset($tag['options']) ? $tag['options'] : '';
        $first = isset($tag['first']) ? $tag['first'] : '';
        $clazz = isset($tag['clazz']) ? $tag['clazz'] : '';
        $style = isset($tag['style']) ? $tag['style'] : '';
        $selected = isset($tag['selected']) ? $tag['selected'] : '';

        $parseStr = '<select';

        if (!empty($id)) {
            $parseStr .= ' id="' . $id . '"';
        }
        if (!empty($name)) {
            $parseStr .= ' name="' . $name . '"';
        }
        if (!empty($clazz)) {
            $parseStr .= ' class="' . $clazz . '"';
        }
        if (!empty($style)) {
            $parseStr .= ' style="' . $style . '"';
        }
        $parseStr .= '>';
        if (!empty($first)) {
            $parseStr .= '<option value="" >' . $first . '</option>';
        }


        if (!empty($options)) {
            if (strstr($options, '#')){
                $parseStr .= '<?php $items = config("'.substr($options, 1).'"); ?>';
            }else{
                $parseStr .= '<?php $items = $'.$options.'; ?>';
            }
            $parseStr .= '<?php if(isset($items)){ foreach($items as $key=>$val) { ?>';
            if ($selected !== '') {
                $parseStr .= '<?php if($' . $selected . ' != null && $' . $selected . ' == $key ) { ?>';
                $parseStr .= '<option selected="selected" value="<?php echo $key ?>"><?php echo $val ?></option>';
                $parseStr .= '<?php }else { ?><option value="<?php echo $key ?>"><?php echo $val ?></option>';
                $parseStr .= '<?php } ?>';
            } else {
                $parseStr .= '<option value="<?php echo $key ?>"><?php echo $val ?></option>';
            }
            $parseStr .= '<?php } }?>';
        }

        $parseStr .= '</select>';
        return $parseStr;
    }

    public function tagCheckbox($tag) {
        $name = $tag['name'];
        $checkboxes = $tag['checkboxes'];
        $checked = $tag['checked'];
        $style = $tag['clazz'];
        $parseStr = '';

        if (!empty($checkboxes)) {
            if (strstr($checkboxes, '#')){
                $parseStr .= '<?php $items = config("'.substr($checkboxes, 1).'"); ?>';
            }else{
                $parseStr .= '<?php $items = $'.$checkboxes.'; ?>';
            }
            $parseStr .= '<?php if(isset($items)){ foreach($items as $key=>$val) { ?>';
            if (!empty($style)) {
                $parseStr .= '<label class="' . $style . '">';
            }
            if (!empty($checked)) {
                $parseStr .= '<?php if(!empty($' . $checked . ') && ($' . $checked . '== $key || (is_array($' . $checked . ') && in_array($key, $' . $checked . '))) ){ ?>';

                $parseStr .= '<input type="checkbox" checked="checked" name="' . $name . '[]" value="<?php echo $key; ?>"><?php echo $val; ?> ';
                $parseStr .= '<?php }else{ ?>';
                $parseStr .= '<input type="checkbox" name="' . $name . '[]" value="<?php echo $key; ?>"><?php echo $val; ?> ';

                $parseStr .= '<?php } ?>';
            }
            if (!empty($style)) {
                $parseStr .= '</label>';
            }
            $parseStr .= '<?php } ?>';
            $parseStr .= '<?php } ?>';
        }

        return $parseStr;
    }

    public function tagRadio($tag) {
        $name = $tag['name'];
        $options = $tag['options'];
        $checked = $tag['checked'];
        $style = $tag['clazz'];
        $parseStr = '';

        if (!empty($options)) {
            if (strstr($options, '#')){
                $parseStr .= '<?php $items = config("'.substr($options, 1).'"); ?>';
            }else{
                $parseStr .= '<?php $items = $'.$options.'; ?>';
            }
            $parseStr .= '<?php if(isset($items)){ foreach($items as $key=>$val) { ?>';
            if (!empty($style)) {
                $parseStr .= '<label class="' . $style . '">';
            }
            if (!empty($checked)) {
                $parseStr .= '<?php if(!empty($' . $checked . ') && ($' . $checked . '== $key || (is_array($' . $checked . ') && in_array($key, $' . $checked . '))) ){ ?>';

                $parseStr .= '<input type="radio" checked="checked" name="' . $name . '[]" value="<?php echo $key; ?>"><?php echo $val; ?> ';
                $parseStr .= '<?php }else{ ?>';
                $parseStr .= '<input type="radio" name="' . $name . '[]" value="<?php echo $key; ?>"><?php echo $val; ?> ';

                $parseStr .= '<?php } ?>';
            }
            if (!empty($style)) {
                $parseStr .= '</label>';
            }
            $parseStr .= '<?php } ?>';
            $parseStr .= '<?php } ?>';
        }

        return $parseStr;
    }

    /**
     * 权限许可标签
     */
    public function tagAuth($tag, $content) {
        $name = $tag['name']; // name是必填项，这里不做判断了

        $parse = '<?php ';
        $parse .= '$__ACTION_RULES__=session("action_rules");'; // 这里是模拟数据
        $parse .= ' ?>';
        $parse .= '{if is_admin() || in_array("'.$name.'", $__ACTION_RULES__)}'.$content.'{/if}';
        return $parse;
    }

}